package com.ruantu.clothing.service.pcpandian;

import com.ruantu.clothing.dao.pcpandian.PCPandianDao;
import com.ruantu.clothing.entity.PageBean;
import com.ruantu.clothing.entity.pcpandian.PCPandianResultA;
import com.ruantu.clothing.entity.pcpandian.ResultSqlMsg;
import com.ruantu.clothing.entity.wxpandian.Pandian;
import com.ruantu.clothing.util.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PCPandianServiceImpl implements PCPandianService {
    @Autowired
    private PCPandianDao pcPandianDao;
    /**
     * 开启新的盘点
     * @param// pdid  盘点ID
     * @param// pandianming 盘点名称
     * @param //start_time 开启时间
     * @param //jg_id 机构ID
     */
    public void startNewPandian(String pdid,String pandianming,String start_time,String jg_id){
        pcPandianDao.startNewPandian(pdid,pandianming,start_time,jg_id);
    }

    /**
     * 查询盘点记录
     * @param jg_id 机构id
     * @return
     */
    public List<Pandian> selectPandianTable(String jg_id,String pdid){
        return pcPandianDao.selectPandianTable(jg_id,pdid);
    }

//==========================================盘点优化============================================================================

    /**
     * 当前盘点查看，包括已盘点和未盘点产品(盘点子表暂无记录)
     * @param selectText
     * @param jg_id
     * @param //start
     * @param //rows
     * @return
     */
 /*   public List<PCPandianResultA> selectNowPandianXQAndPdzbIsNull(String selectText,String jg_id,PageBean pageBean){
        pageBean.setTotal(pcPandianDao.selectNowPandianXQAndPdzbIsNullCounts(selectText,jg_id));
        return pcPandianDao.selectNowPandianXQAndPdzbIsNull(selectText,jg_id,pageBean.getStart(),pageBean.getRows());
    }*/


    /**
     * 当前盘点查看，包括已盘点和未盘点产品(盘点子表已存在记录)
     * @param selectText
     * @param jg_id
     * @param //pdid
     * @param //start
     * @param// rows
     * @return
     */
    public List<PCPandianResultA> selectNowPandianXQAndPdzbIsExist(String selectText,String jg_id,PageBean pageBean){
        pageBean.setTotal(pcPandianDao.selectNowPandianXQAndPdzbIsExistCounts(selectText,jg_id));
        return pcPandianDao.selectNowPandianXQAndPdzbIsExist(selectText,jg_id,pageBean.getStart(),pageBean.getRows());
    }

    /**
     * 查询当前盘底是否已经存在盘点子表记录(0为没有)
     * @param //pdid
     * @return
     */
    public Integer selectPdjlByPdid(String jg_id){
        return pcPandianDao.selectPdjlByPdid(jg_id);
    }
    //=============================================================================================================================

    /**
     * 根据条件获取当前盘点总数，包括已盘点和未盘点
     * @param selectText
     * @param jg_id
     * @return
     */
    /*public Integer selectTotalCountsNowPD (String selectText,String jg_id){
        return pcPandianDao.selectTotalCountsNowPD(selectText,jg_id);
    }*/

    /**
     *根据盘点ID查询本机构已盘点详情
     * @param pandian_id 盘点id
     * @param selectText 查询条件
     * @param pageBean
     * @return
     */
    public List<PCPandianResultA> selectAlreadyPDXQByPdid(String pandian_id,String selectText,PageBean pageBean){
        pageBean.setTotal(pcPandianDao.selectTotalCountAlreadyPDid(pandian_id,selectText));
        return pcPandianDao.selectAlreadyPDXQByPdid(pandian_id,selectText,pageBean.getStart(),pageBean.getRows());
    }

    /**
     * 获取当前正在盘点id（用于判断当前盘点是否正在进行）
     * @param jg_id
     * @return
     */
    public String selectNowPDID(String jg_id){
        return pcPandianDao.selectNowPDID(jg_id);
    }

    /**
     * 结束盘点——修改盘点结束时间以及状态
     * @param endime
     * @param pdid
     */
    @Transactional
    public ResultSqlMsg pandianEnd(String endime, String pdid){
        ResultSqlMsg code=new ResultSqlMsg();
        code.setCode1(pcPandianDao.updatePandianTable(endime,pdid));
        code.setCode2(pcPandianDao.updateStoreCounts(pdid));
        System.out.println(code);
        if(code.getCode1()<=0||code.getCode2()<=0){
            throw new MyException("结束盘点失败");
        }
        return code;
    }
}
